<template>
  <div>
    <!--服务活动查询-厂端-->
    <TopFilter ref="topfilter" :list="topFilterList" :cols="4" labelWidth="100" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :onSyncTableData="tableChange"
      :isExportExcel="false"
      exportFileName="服务活动查询.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" >
        <multiuse-button type="primary" :click="exportHandle.bind(this)">导出</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visibleDetail" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detail :maId="maId" @close="closeHandler" :handleType="handleType" />
    </Drawer>

    <Drawer :visible.sync="addVinShow" :title="'更新vin'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addVin :data="maInfo" @close="closeHandler" :handleType="'add'" />
    </Drawer>

    <Drawer :visible.sync="deleteVinShow" :title="'删除vin'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <deleteVin :data="maInfo" @close="closeHandler" :handleType="'add'" />
    </Drawer>

    <BaseDialog :visible.sync="visibleCar" title="活动车辆列表" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" destroyOnClose>
      <sport-car :maId="maId" :flag="flag" @closeCar="closeHandler"></sport-car>
    </BaseDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import detail from './detail';
import sportCar from './sportCar';
import addVin from './addVin';
import deleteVin from './deleteVin';
import { isEmpty } from '@/utils/utils';
import { getAllUser } from '@/api/repair/batchEventManagement/activityDistribution';
import { searchListRecord, exportSearchListRecord, isReturnChange, isClaimChange,exporturl } from '@/api/repair/batchEventManagement/activityQueryCDuan';
import { arrayObjDeWeight } from '@/utils';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { getAppId } from '@/assets/js/auth';
import { notifyAction } from '../../../../utils';

export default {
  name: 'addItem',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      visibleDetail: false,
      visibleCar: false,
      fetchApi: searchListRecord,
      dialogTitle: '',
      deleteVinShow: false,
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      maInfo: {},
      addVinShow: false,
      maId: '',
      /*应执行传递空窜 已执行传递98161003*/
      flag: ''
    };
  },
  components: {
    detail,
    sportCar,
    addVin,
    deleteVin
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;

    this.initSelect();

    this.spliceCloum();
  },
  methods: {
    async exportHandle() {

      const [err, params] = await this.$refs.topfilter.GET_FORM_DATA();

      await this.downHandleFile(exportSearchListRecord, params, '服务活动查询');
    },

    tableChange(data) {
      data.forEach((item, index) => {
        // item.maBy=Number(item.maBy)
      });
    },

    // 导出处理函数
    handleExportFun(row) {
      debugger
      //状态
      row.maStatus = this.createDictText(row.maStatus, 1014); //10141001, 10141005, 10141006
      //类型
      row.maStrategy = this.createDictText(row.maStrategy, 5080);

           this.getAllUsers(this.columns, ['row.maBy'], 'table');


    },

    async initSelect() {
      // let res = await getAllUser();
      // // 去重
      // let dataList = arrayObjDeWeight(res.data, 'userId');
      //
      // this.columns.find(item => item.dataIndex === 'maBy').dictItems = dataList.map(x => ({
      //   text: x.employeeName,
      //   value: x.userId
      // }));

      this.getAllUsers(this.columns, ['maBy'], 'table');
    },
    spliceCloum() {
      let appId = getAppId().toLowerCase();
      if (appId == 'arcfox') {
        this.columns.splice(7, 1);
      }
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          render: props => {
            let obj = props.row;

            let dataSource = isEmpty(obj['dataSource']) ? '' : obj['dataSource'];
            let maStatus = isEmpty(obj['maStatus']) ? '' : obj['maStatus'];

            let rst = false;
            if (dataSource == '10151001' && maStatus == '10141001') {
              rst = true;
            }
            return (
              <div>
                <el-button size="small" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </el-button>


              </div>
            );
          }
        },
         {
          title: '活动编号',
          dataIndex: 'maNo',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动名称',
          dataIndex: 'maTitle',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动类型',
          dataIndex: 'maStrategy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('5080'),
          render: props => {
            return <span>{this.createDictText(props.row.maStrategy,'5080')}</span>
          }

        },
           {
          title: '活动开始日期',
          dataIndex: 'bDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '活动截止日期',
          dataIndex: 'eDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
       {
          title: '发布人',
          dataIndex: 'maBy',
          sorter: true,
          dictItems: []
        },
        {
          title: '发布日期',
          dataIndex: 'maDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '更新日期',
          dataIndex: 'updatedAt',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '活动状态',
          dataIndex: 'maStatus',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1014,[10141001, 10141006]),
          dictItems: this.createDictList('1014'),
          render: props => {
            return <span>{this.createDictText(props.row.maStatus, '1014')}</span>;
          }
        },

      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'maNo',
          placeholder: '活动编号...'
        },
        {
          type: 'INPUT',
          label: '活动名称',
          fieldName: 'maTitle',
          placeholder: '活动名称...'
        },
        {
          type: 'RANGE_DATE',
          label: '活动开始日期',
          dateType: 'daterange',
          style: { minWidth: '200px' },
          fieldName: 'baDate|bbDate',
          rangeSeparator:'至'

        },
       {
          type: 'RANGE_DATE',
          label: '活动结束日期',
          dateType: 'daterange',
          style: { minWidth: '200px' },
          fieldName: 'eaDate|ebDate',
          rangeSeparator:'至'

        },
         {
          type: 'SELECT',
          label: '活动状态',
          fieldName: 'maStatus',
          placeholder: '请选择',
          itemList: this.createDictList(1014, [10141001, 10141006,10141004]) //待下发、已下发、已关闭展示[10141001, 10141005, 10141006]
        }

      ];
    },
    isEdit(row) {},
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    /*flag 应执行传递空窜 已执行传递98161003*/
    findHandle(flag, row) {
      this.maId = row['maId'];
      this.flag = flag;
      this.visibleCar = true;
    },

    detailHandle(row) {
      this.maId = row['maId'];
      this.handleType = 'show';
      this.showHandler();
    },
    changeHandle(val) {
      // val.bDate=moment(val['bDate']).format('YYYY-MM-DD')
      // val.eDate=moment(val['eDate']).format('YYYY-MM-DD')
      this.fetchParams = Object.assign({ xhrAbort: false }, val);

      console.log(this.fetchParams);
    },
    showHandler(type = 0) {
      this.dialogTitle = `${this.conf[this.handleType]}活动查询-厂端`;

      this.visibleDetail = true;
    },
    closeHandler(type) {
      this.visibleCar = false;
      this.visible = false;
      this.visibleDetail = false;
      this.addVinShow = false;
      this.deleteVinShow = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    //添加vin方法
    addVin(val) {
      this.maInfo = val;
      this.addVinShow = true;
    },
    //删除vin方法
    deleteVin(val) {
      this.maInfo = val;
      this.deleteVinShow = true;
    },
    //结算更改方法
    async isClaimChange(val) {
      let res = await isClaimChange({ maId: val.maId });
      if (res.resultCode == 200) {
        notifyAction('更改成功', 'success');
        this.fetchParams = Object.assign({ xhrAbort: false }, this.fetchParams);
      }
    },
    //返件方法
    async isReturnChange(val) {
      let res = await isReturnChange({ maId: val.maId });
      if (res.resultCode == 200) {
        notifyAction('更改成功', 'success');
        this.fetchParams = Object.assign({ xhrAbort: false }, this.fetchParams);
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
